{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "82ab0ed0",
   "metadata": {},
   "source": [
    "-------\n",
    "* 学习人：罗海鑫\n",
    "* 时间：2021-12-06\n",
    "* week15\n",
    "* 学习线上资源：[python官方文档](https://www.python.org)\n",
    "* [博客_函数模块](https://cnblogs.com/xuzhichao/p/11570285.html)\n",
    "* [python-官方文档-函数模块](https://docs.python.org/3/tutorial/controlflow.html#definding-functions)\n",
    "-------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eae38154",
   "metadata": {},
   "source": [
    "## 上周回顾\n",
    "> 1. 函数的定义：def\n",
    "> 2. 函数名的定义规则 -> 变量名一样的规则\n",
    "> 3. 调用函数的方式\n",
    "> 4. 参数：\n",
    ">> 1. 形式参数（定义函数）\n",
    ">> 2. 实际参数（调用函数）\n",
    ">> 3. 有多个参数时：\n",
    ">>> 0. 关键字参数\n",
    ">>> 1. 位置参数（调用函数时） --> 关键字参数均要放在末尾（多个不考虑顺序）\n",
    ">>> 2. 默认参数（定义函数时） --> 默认参数均放在末尾（多个不考虑顺序）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "765fadc8",
   "metadata": {},
   "source": [
    "# 本周知识\n",
    "> 1. 函数的返回值\n",
    "> 2. 函数的参数类型\n",
    "> 3. 模块与函数的关系\n",
    "> 4. 定义一个模块，然后调用模块\n",
    ">> 1. 调用模块\n",
    ">> 2. 调用模块中的函数\n",
    "> 5. 模块与函数实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff186e83",
   "metadata": {},
   "source": [
    "## 1.函数的返回值\n",
    "\n",
    "> 1. 函数一定有返回值（无论是什么编程语言）\n",
    "> 2. 在其他的编程语言中（如：c语言，Java等），返回值一定要写，不写会报错！对于python（更高级的语言），返回值如果不写，给定默认值None\n",
    "> 3. 返回值一般为函数体运行的结果（result）：也可以是自定义的函数返回值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "0b2cf505",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数返回实践1\n",
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + ' '+ last_name\n",
    "#     print(full_name)\n",
    "    return full_name\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "791def31",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Li Hua\n"
     ]
    }
   ],
   "source": [
    "print(get_formatted_name(\"Li\",\"Hua\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "687be1b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def log_in(username,password):\n",
    "    \"\"\"验证登录信息\"\"\"\n",
    "    if username == \"Lihua\" and password == 123456:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "48be9de3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_in(\"Lihua\",123456)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dabbfe7a",
   "metadata": {},
   "source": [
    "## 2.函数的参数\n",
    "### 1.函数输入的参数\n",
    "> 1. 函数可以输入复杂的参数\n",
    ">> 1. 函数参数为列表\n",
    ">> 2. 函数参数为字典\n",
    "> 2. 函数可以输入不确定个数的参数：\n",
    ">> 1. *args\n",
    ">> 2. **kwargs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "82498d46",
   "metadata": {},
   "outputs": [],
   "source": [
    "def list_test(names):\n",
    "    \"\"\"测试输入参数为列表\"\"\"\n",
    "    print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "6a7be3a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Lihua', 'Xiaoming', 'Hai']\n"
     ]
    }
   ],
   "source": [
    "list_test([\"Lihua\",\"Xiaoming\",\"Hai\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3afff4e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def dict_test(request_header):\n",
    "    \"\"\"测试输入参数为字典\"\"\"\n",
    "    print(request_header)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "af7d54f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# http://httpbin.org/get\n",
    "httpbin_get = {\n",
    "  \"args\": {}, \n",
    "  \"headers\": {\n",
    "    \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\", \n",
    "    \"Accept-Encoding\": \"gzip, deflate\", \n",
    "    \"Accept-Language\": \"zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6\", \n",
    "    \"Host\": \"httpbin.org\", \n",
    "    \"Upgrade-Insecure-Requests\": \"1\", \n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.43\", \n",
    "    \"X-Amzn-Trace-Id\": \"Root=1-61adb9c4-25771d337bfda0d52ef461e6\"\n",
    "  }, \n",
    "  \"origin\": \"223.104.67.67\", \n",
    "  \"url\": \"http://httpbin.org/get\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "889bddb8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',\n",
       " 'Accept-Encoding': 'gzip, deflate',\n",
       " 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',\n",
       " 'Host': 'httpbin.org',\n",
       " 'Upgrade-Insecure-Requests': '1',\n",
       " 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.43',\n",
       " 'X-Amzn-Trace-Id': 'Root=1-61adb9c4-25771d337bfda0d52ef461e6'}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "httpbin_get['headers']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "5a6f0d22",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6', 'Host': 'httpbin.org', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.43', 'X-Amzn-Trace-Id': 'Root=1-61adb9c4-25771d337bfda0d52ef461e6'}\n"
     ]
    }
   ],
   "source": [
    "dict_test(httpbin_get['headers'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e75586b8",
   "metadata": {},
   "source": [
    "> 2. 函数可以输入不确定个数的参数：\n",
    ">> 1. *args\n",
    ">>> 1. args 可以接受多个数据（单值）\n",
    ">>> 2. args 将多个数据存在一个**元组**当中，实际还是一个变量\n",
    ">> 2. **kwargs\n",
    ">>> 1. kwargs 可以接受多个数据（键值对）==> 关键字参数\n",
    ">>> 2. kwargs 将多个 值键对 的数据存放在一个 字典 中，实际上还是一个变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "55e8f77d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def food_ingred(food,*ingreds):\n",
    "    \"\"\"食物的配料单\"\"\"\n",
    "    return food,ingreds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "1aeadaf1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('pizza', ('mushroom', 'green peppers', 'extra chess'))"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "food_ingred(\"pizza\",\"mushroom\",\"green peppers\",\"extra chess\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "1cfb69d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# **kwargs\n",
    "def person(stud_name,**stud_info):\n",
    "    \"\"\"学生个人信息完善表\"\"\"\n",
    "    return stud_name,stud_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "64ae9c11",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Lihua', {'age': 18, 'job': 'student', 'address': '广州南方学院'})"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person(\"Lihua\",age=18,job=\"student\",address=\"广州南方学院\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27b80122",
   "metadata": {},
   "outputs": [],
   "source": [
    "def func_name(arg1,arg2,*arg,**kwargs):\n",
    "    \"\"\"考虑比较周到的函数\"\"\"\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15a5f022",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "21faca83",
   "metadata": {},
   "source": [
    "### 2.函数输出的返回值\n",
    "\n",
    "> 1. 返回值可以是字符串、数值、列表、字典等数据（单一值）\n",
    "> 2. 返回值是唯一的，不可以有两个（这是其他语言，如c语言，Java等）\n",
    "> 3. python可以输出多个返回值（的返回值可有多个不同的值），用元组的形式进行封装并返回。（本质不违背函数的定义）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1b86e2e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test1_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return \"你好\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "2a5d0d64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "9c9a1ac8",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test2_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "91c72c91",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test2_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "4cb940f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test3_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return [1,2,3,4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "7f6e9d21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function __main__.test3_return()>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test3_return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "d436b04d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test4_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return httpbin_get['headers']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "b8c88ae2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',\n",
       " 'Accept-Encoding': 'gzip, deflate',\n",
       " 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',\n",
       " 'Host': 'httpbin.org',\n",
       " 'Upgrade-Insecure-Requests': '1',\n",
       " 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.43',\n",
       " 'X-Amzn-Trace-Id': 'Root=1-61adb9c4-25771d337bfda0d52ef461e6'}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test4_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "53fab4b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试多个值\n",
    "\n",
    "def test5_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return 1,2,\"Lihua\",[1,2,3],{\"name\":\"Alex\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "2e53f862",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 'Lihua', [1, 2, 3], {'name': 'Alex'})"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test5_return()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f48c059a",
   "metadata": {},
   "source": [
    "## 3.模块\n",
    "\n",
    "> 1. 使用模块可以隐藏函数的源代码\n",
    "> 2. 使用的人不需要会底层代码（只需要会使用模块即可）\n",
    ">> 1. 必须先调用模块，才可以使用\n",
    ">>> 1. import moudle_name 直接调用整个模块，此时模块的所有函数处于备用状态，使用的方法为：moudle_name.func_name()\n",
    ">>> 2. from moudle_name import func_name,此时，仅调用模块的某一个函数，只能使用这个模块的该函数，使用方法为 func_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "b70077ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "60d7436c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=12, tm_mday=6, tm_hour=16, tm_min=35, tm_sec=22, tm_wday=0, tm_yday=340, tm_isdst=0)"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "8f41435f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 调用模块中函数的方法一，调用整个模块\n",
    "import study"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "450981c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o'}"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "study.search4vowels(\"hello zhichao\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "ec1691e6",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'study' has no attribute 'news_链接'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-50-7c00a969a59c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mhelp\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstudy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnews_链接\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: module 'study' has no attribute 'news_链接'"
     ]
    }
   ],
   "source": [
    "help(study.news_链接)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "16071c4f",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'news_链接' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-51-90099005e57d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstudy\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mnews_链接\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"https://www.nfu.edu.cn/\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"xydt\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"96\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'news_链接' is not defined"
     ]
    }
   ],
   "source": [
    "study,news_链接(\"https://www.nfu.edu.cn/\",\"xydt\",\"96\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c5e4826",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 调用模块中函数的方法二，调用模块中的某一个函数\n",
    "from study import search4vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1d552c0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "search4vowels(\"shudugdheyug\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3de55167",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c19ae1f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
